package com.xiaoyao.novel.action.page;

import java.util.List;

import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.util.HtmlStatic;
import com.xiaoyao.novel.util.SpendClock;
import com.xiaoyao.novel.velocity.PageVelocityStatic;
import com.xiaoyao.novel.velocity.PhoneVelocityStatic;

public class StaticAction extends NovelAction {
	private static final long serialVersionUID = 1L;
//	StaticService staticService = new StaticService();
	int bookId;
	private int staticCount = 10000;

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	public String staticIndex() throws Exception {
		PageVelocityStatic.staticIndex() ;
//		HtmlStatic.staticIndex();
		request.setAttribute("msg", "首页静态化成功!");
		return SUCCESS;
	}
	public String staticCategory() throws Exception {
//		List<Category> list = categoryService.getAll();
//		HtmlStatic.staticCategory(list);
 
		PageVelocityStatic.staticCategory();
		request.setAttribute("msg", "首页小说分类成功!");
		return SUCCESS; 
	}

	public String staticBooks() throws Exception {
//		List<Book> bookList = bookService.getAllBook();
//		HtmlStatic.staticBooks(bookList);
		
		PageVelocityStatic.staticBooks();
		request.setAttribute("msg", "所有小说静态化成功!");
		return SUCCESS;
	}

	public String staticTopChapter() throws Exception {
//		staticService.staticTopNChapter(staticCount); 
		PageVelocityStatic.staticBookAndChapters(staticCount); 
		request.setAttribute("msg", "所有小说最新" + staticCount + "章静态化成功!");
		return SUCCESS;
	}
	public String staticChapters() throws Exception {
		PageVelocityStatic.staticBookAndChapters();
//		List<Book> bookList = bookService.getAll();
//		for (Book book : bookList) {
//			Pagin pagin = new Pagin();
//			pagin.setPageSize(10000);
//			pagin.getSearchParam().put("bookId", "" + book.bookId);
//			List<Chapter> chapters = chapterService.search(pagin);
//			for (Chapter chapter : chapters) {
//				HtmlStatic.staticChapter(chapter, false);
//			}
//		}
		request.setAttribute("msg", "所有小说章节静态化成功!");
		logger.info("所有小说章节静态化成功!");
		return SUCCESS;
	}
 

	public String staticBook() throws Exception {
		Book book = bookService.getById(bookId);
//		HtmlStatic.staticBook(book);
//		HtmlStatic.staticChapters(bookId);
		PageVelocityStatic.staticBookAndChapters(book, staticCount);
		request.setAttribute("msg", "小说静态化成功!" + book.getBookName());
		return SUCCESS;
	}

	public String removeChapters() {
		int size = HtmlStatic.removeChapters();
		request.setAttribute("msg", "删除所有静态章节成功,数量:" + size);
		return SUCCESS;
	}

	public String removeBooks() {
		HtmlStatic.removeBooks();
		List<Book> bookList = bookService.getAll();
		HtmlStatic.staticBooks(bookList);
		request.setAttribute("msg", "删除所有静态书籍并重新静态化成功!");
		return SUCCESS;
	}
 
	// /////////////////////

	// /////////////////////

	public String staticPhoneIndex() throws Exception {
		SpendClock.start();
		PhoneVelocityStatic.staticIndex();
		long spend = SpendClock.getSpend();
		logger.warn("手机首页静态化耗时:" + spend + " ms ");
		request.setAttribute("msg", "手机首页静态化成功!");
		return SUCCESS;
	}

	public String staticPhoneCategory() throws Exception {
		SpendClock.start();
		PhoneVelocityStatic.staticCategory();
		long spend = SpendClock.getSpend();
		logger.warn("所有手机小说静态化耗时:" + spend + " ms ");
		request.setAttribute("msg", "手机小说分类成功!");
		return SUCCESS;
	}

	public String staticPhoneBooks() throws Exception {
		SpendClock.start();
		PhoneVelocityStatic.staticBooks();
		long spend = SpendClock.getSpend();
		logger.warn("所有手机小说静态化耗时:" + spend + " ms ");
		request.setAttribute("msg", "所有手机小说书籍页静态化成功!");
		return SUCCESS;
	}

	public String staticPhoneChapter() throws Exception {
		PhoneVelocityStatic.staticBookAndChapters(staticCount);
		request.setAttribute("msg", "所有手机小说最新" + staticCount + "章静态化成功!");
		return SUCCESS;
	}

	// /////////////////////

	// /////////////////////

	// /////////////////////

	// /////////////////////

	public int getStaticCount() {
		return staticCount;
	}

	public void setStaticCount(int staticCount) {
		this.staticCount = staticCount;
	}

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

}
